package com.lwd.controller;

import java.io.IOException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lwd.entity.Type;
import com.lwd.service.TypeService;

import javax.servlet.http.HttpServletResponse;

@Controller
public class TypeController {
    @Autowired
    private TypeService typeService;

//    @RequestMapping("/")
//    public void redirectGetAllTypeGoods(HttpServletResponse response) throws IOException {
//        response.sendRedirect("getAllTypeGoods");
//    }

    @RequestMapping("/aaa")
    public String aaa() {
        return "redirect:getAllTypeGoods";
    }

    //	@ResponseBody
    @RequestMapping("getAllTypeGoods")
    public String getAllTypeGoods(Model model) {

        List<Type> typeGoods = typeService.getAllTypeGoods();

        model.addAttribute("typeGoods", typeGoods);
        return "index";
    }

    @ResponseBody
    @RequestMapping("getAllTypeGoods2")
    public List<Type> getAllTypeGoods2(Model model) {

        List<Type> typeGoods = typeService.getAllTypeGoods2();

//		model.addAttribute("typeGoods", typeGoods);
        return typeGoods;
    }

    @RequestMapping("getAllTypes")
    public String getAllTypes(Model model) {

        List<Type> list = typeService.getAllTypes();
        model.addAttribute("typeList", list);
        return "category";
    }

    @RequestMapping("addNewType")
    public String addNewType(Type type) {

        boolean flag = typeService.addNewType(type);
        if (flag) {
            return "redirect:getAllTypes";
        } else {
            return "failure";
        }

    }
}
